function togglesearch(){
	if(document.getElementById('advancedsearch').style.display === 'block'){
		document.getElementById('advancedsearch').style.display = 'none';
	} else {
		document.getElementById('advancedsearch').style.display = 'block';		
	}
}

function togglesearchoption(){
	if((document.getElementById('searchoption').value)==='all'){
		document.getElementById('query_username').style.display = 'inline';
		document.getElementById('query_title').style.display = 'inline';
		document.getElementById('query_tag').style.display = 'inline';
	} else if((document.getElementById('searchoption').value)==='username'){
		document.getElementById('query_username').style.display = 'inline';
		document.getElementById('query_title').style.display = 'none';
		document.getElementById('query_tag').style.display = 'none';
	} else {
		document.getElementById('query_username').style.display = 'none';
		document.getElementById('query_title').style.display = 'inline';
		document.getElementById('query_tag').style.display = 'inline';
	}
}

function toggleachievement(){
	if(document.getElementById('userachievement').style.display === 'block'){
		document.getElementById('userachievement').style.display = 'none';
	} else {
		document.getElementById('userachievement').style.display = 'block';		
	}
}

function checkLogin()
{
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText==='0')
				document.getElementById("loginrespond").innerHTML="Login failed! Wrong username or password.";
			else
				window.location = "user.php?username="+document.getElementById("login_username").value;
		}
	}
	xmlhttp.open("POST","login.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	var qry = "login_username="+document.getElementById("login_username").value+"&login_password="+document.getElementById("login_password").value;
	xmlhttp.send(qry);
}

function enter_checkLogin(e){
	if(e.keyCode==13)
		checkLogin();
}

function checkUsernameEmail()
{
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){			
			var respond = xmlhttp.responseText.split(" ");			
			if(respond[0]==0)
				document.getElementById("username_respond").innerHTML="Username is exist. Choose different username!";
			else
				document.getElementById("username_respond").innerHTML="";
			if(respond[1]==0)
				document.getElementById("email_respond").innerHTML="E-mail is exist. Choose different e-mail!";
			else
				document.getElementById("email_respond").innerHTML="";
		}
	}
	xmlhttp.open("POST","register_validator.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	var qry = "username="+document.getElementById("username").value+"&email="+document.getElementById("email").value;
	xmlhttp.send(qry);
}

var radioselected = 1;
function enableLink(){    
        clearVideoPost();
	document.getElementById('link').disabled=false;
	document.getElementById('linkdesc').disabled=false;
	document.getElementById('imagefile').disabled=true;
	document.getElementById('videourl').disabled=true;
	radioselected = 1;
}
function enableImage(){
        clearLinkPost();
        clearVideoPost();
	document.getElementById('link').disabled=true;
	document.getElementById('linkdesc').disabled=true;
	document.getElementById('imagefile').disabled=false;
	document.getElementById('videourl').disabled=true;
	radioselected = 2;
}
function enableVideo(){
        clearLinkPost();
	document.getElementById('link').disabled=true;
	document.getElementById('linkdesc').disabled=true;
	document.getElementById('imagefile').disabled=true;
	document.getElementById('videourl').disabled=false;
	radioselected = 3;
}
function disableAll(){
	document.getElementById('link').disabled=true;
	document.getElementById('linkdesc').disabled=true;
	document.getElementById('imagefile').disabled=true;
	document.getElementById('videourl').disabled=true;
	radioselected = 0;
}

function clearLinkPost(){
    document.getElementById('link').value='';
    document.getElementById('linkdesc').value = '';
}

function clearVideoPost(){
    document.getElementById('videourl').value='';
}

function showPreview(){
	var title = '<p style="font-weight: bold;font-size: 30px;">' + document.getElementById('title').value + '</p>';
	var postcontent;
	var d = new Date();
	var date = d.getDate();
	var month = d.getMonth();
	var year = d.getFullYear();
	var time = d.toLocaleTimeString();
	var postingtime = date + '/' + month + '/' + year + ' ' + ' at ' + time;
	        
	switch(radioselected){
		case 1:{
				//link
				postcontent = title + 
				'<div><span>posted by </span><span id="content-poster" class="commentator">Anonymous</span><div class="dateformat">' + postingtime + '</div></div>'
				+ '<p style="left-margin:30px;">' + document.getElementById('linkdesc').value + '<br/>URL : <a href="' + document.getElementById('link').value + '">' + document.getElementById('link').value + '</a></p>';
                                document.getElementById('preview').innerHTML = postcontent;
				break;
		}
		case 2:{
                                //DO NOTHING
				/*                                
                                var newimg = document.createElement("IMG");
				var fileimg = document.getElementById('imagefile');
				var imgfiles = fileimg.value;
				alert(imgfiles);
				alert('oooi');
				document.getElementById('preview').appendChild(newimg);
				var imgurl = "tenggelam.jpg";
				postcontent = title + 
				'<div><span>posted by </span><span id="content-poster" class="commentator">Anonymous</span><div class="dateformat">' + postingtime + '</div></div>'
				+ '<div class="imagevideo"><img src="' + imgurl + '" width="420" height="345" alt="preview_picture"></img></div>';
				break;
                                */
                               break;
		}
		case 3:{
				//video
				var url = document.getElementById('videourl').value;
				var newurl = url.toString().replace("watch?v=", "v/");
				postcontent = title + 
				'<div><span>posted by </span><span id="content-poster" class="commentator">Anonymous</span><div class="dateformat">' + postingtime + '</div></div>'
				+ '<div class="imagevideo"><embed width="420" height="345" src="' + newurl + '" type="application/x-shockwave-flash"></embed></div>';
                                document.getElementById('preview').innerHTML = postcontent;
				break;
		}
		default:{
				break;
		}
	}	
}

function changeLike(id, like, labelid, change) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
		
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {			
			document.getElementById(labelid).innerHTML = xmlhttp.responseText;
		}
	}
		
	var url = "getlike.php";
	url = url + "?id=" + id + "&like=" + like + "&change=" + change;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function comment(id) {
	var xmlhttp;
	var usercomment = document.getElementById("commenttxt").value;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
		
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {				
			document.getElementById("commentdiv").innerHTML = xmlhttp.responseText;
		}
	}
	
	var url = "getcomment.php";
	url = url + "?id=" + id + "&comment=" + usercomment;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function deleteComment(comid, conid) {
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
		
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {				
			document.getElementById("commentdiv").innerHTML = xmlhttp.responseText;
		}
	}
	
	var url = "deletecomment.php";
	url = url + "?comid=" + comid + "&conid=" + conid;
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}